//


#import "NT_AddressAddViewController.h"

@interface NT_AddressAddViewController ()

/** */
@property (nonatomic, strong) NT_CoreDataBase *data;

@end

@implementation NT_AddressAddViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    PostNotifition(@"Hidden");
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    PostNotifition(@"Appear");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setnavBatTitle:@"Please add content and return"];
    _data = [[NT_CoreDataBase alloc]initWithCoreData:@"AddressModel" modelName:@"NoteData" success:^{} fail:^(NSError * _Nullable error) {}];
    
    [self creatUI];
}

- (void)creatUI {
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, UI_NAV_BAR_HEIGHT, kMainScreenWidth, kRelativeHeight(40))];
    label.textColor = rgba(2, 2, 2, 1);
    label.font = FontPFMedium(kRelativeWidth(17));
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"Save the input address";
    [self.view addSubview:label];
    
    NSArray *array = @[@"Please enter name",@"Please enter the phone num",@"Please enter area",@"Please enter the house number"];
    for (int a = 0; a < 4; a++) {
        UITextField *textfield = [[UITextField alloc]initWithFrame:CGRectMake(kRelativeWidth(20), UI_NAV_BAR_HEIGHT+kRelativeHeight(40)+(kRelativeHeight(50)*a), kMainScreenWidth - kRelativeWidth(40), kRelativeHeight(40))];
        textfield.textColor = [UIColor lightGrayColor];
        textfield.font = FontPFRegular(kRelativeWidth(15));
        textfield.tag = a+1;
        textfield.placeholder = array[a];
        [self.view addSubview:textfield];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(kRelativeWidth(20), CGRectGetMaxY(textfield.frame), kMainScreenWidth - kRelativeWidth(40), 1)];
        lineView.backgroundColor = [UIColor lightGrayColor];
        [self.view addSubview:lineView];
        
        switch (a) {
            case 0:
                textfield.text = _model.name;
                break;
            case 1:
                textfield.text = _model.phone;
                break;
            case 2:
                textfield.text = _model.area;
                break;
            case 3:
                textfield.text = _model.detail;
                break;
                
            default:
                break;
        }
        
    }
    UITextField *views = [self.view viewWithTag:4];
    UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(kRelativeWidth(78), CGRectGetMaxY(views.frame) + kRelativeHeight(102), kRelativeWidth(220), kRelativeHeight(40))];
    [btn setTitleColor:rgba(255, 255, 255, 1) forState:UIControlStateNormal];
    [btn setBackgroundColor:rgba(255, 100, 0, 1)];
    [btn setTitle:@"Submit" forState:UIControlStateNormal];
    btn.titleLabel.font = FontPFRegular(kRelativeWidth(16));
    btn.layer.cornerRadius = kRelativeWidth(18);
    [btn addTarget:self action:@selector(btndidClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
}
//MARK:保存数据
- (void)btndidClick {
    UITextField *views1 = [self.view viewWithTag:1];
    UITextField *views2 = [self.view viewWithTag:2];
    UITextField *views3 = [self.view viewWithTag:3];
    UITextField *views4 = [self.view viewWithTag:4];
    
    if (views1.text.length <= 0 ||
        views2.text.length <= 0 ||
        views3.text.length <= 0 ||
        views4.text.length <= 0) {
        return;
    }
    
    NSMutableDictionary *dic = [NSMutableDictionary new];
    [dic setObject:views1.text forKey:@"name"];
    [dic setObject:views2.text forKey:@"phone"];
    [dic setObject:views3.text forKey:@"area"];
    [dic setObject:views4.text forKey:@"detail"];
    
    if (_model == nil) {
        [dic setObject:[self randomhhhhhhhhhh] forKey:@"id"];
        [_data ntInsertNewEntity:dic success:^{
            [self.navigationController popViewControllerAnimated:YES];
            
        } fail:^(NSError * _Nullable error) {}];
        
    }else {
        NSLog(@"%@",_model.id);
        [_data ntUpdateEntity:dic key:[NSString stringWithFormat:@"id = %@",_model.id] success:^{
            [self.navigationController popViewControllerAnimated:YES];
        } fail:^(NSError * _Nullable error) {}];
    }
    
    
}
- (NSString *)randomhhhhhhhhhh
{
    const int count =6;
    NSString * sourceString=@"678912345";
    NSMutableString *rehhhh=[[NSMutableString alloc]init];
    for (int i = 0; i < count; i++) {
        unsigned index=rand()%[sourceString length];
        NSString * s=[sourceString substringWithRange:NSMakeRange(index, 1)];
        [rehhhh appendString:s];
        NSLog(@"id-------%@",rehhhh);
    }
    return rehhhh;
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
